import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';
import 'package:flutterdemo/utils/date_util.dart';
import 'package:video_player/video_player.dart';

class VideoPlayerPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _VideoPlayerPageState();
  }
}

class _VideoPlayerPageState extends State<VideoPlayerPage> {
  VideoPlayerController _videoPlayerController;
  bool _isPlaying = false;
  String _playTime = "00:00";
  String _totalTime = "00:00";
  double _slideValue = 0.0;
  int _totalTimeSec = 0;

  @override
  void initState() {
//    _videoPlayerController = VideoPlayerController.network(
//        'http://vfx.mtime.cn/Video/2019/03/21/mp4/190321153853126488.mp4');
    _videoPlayerController = VideoPlayerController.asset('assets/video1.mp4');
//    _videoPlayerController = VideoPlayerController.file(
//        File('/storage/emulated/0/DCIM/Camera/VID_20200626_083137.mp4'));
    _videoPlayerController.addListener(() {
      _videoPlayerController.position.then((value) {
        setState(() {
          bool isPlaying = _videoPlayerController.value.isPlaying;
          if (_isPlaying != isPlaying) {
            _isPlaying = isPlaying;
          }
//          _slideValue = value.inSeconds /
//              _videoPlayerController.value.duration.inSeconds *
//              100;
          _playTime = DateUtil.secondsFormat(value.inSeconds, [nn, ":", ss]);
        });
      });
    });
    _videoPlayerController.initialize().then((value) => setState(() {
          _videoPlayerController.value.isLooping;
          _totalTimeSec = _videoPlayerController.value.duration.inSeconds;
          print('视频参数:${_videoPlayerController.value.toString()},'
              '时间${_videoPlayerController.value.duration.inSeconds}');
          _totalTime = DateUtil.secondsFormat(
              _videoPlayerController.value.duration.inSeconds, [nn, ":", ss]);
        }));
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: double.infinity,
        height: double.infinity,
        color: Colors.black,
        child: Stack(
          children: [
            Center(
              child: AspectRatio(
                aspectRatio: _videoPlayerController.value.aspectRatio,
                child: VideoPlayer(_videoPlayerController),
              ),
            ),
            Align(
              alignment: Alignment.bottomCenter,
              child: Container(
                height: 60,
                margin: EdgeInsets.all(10),
                padding: EdgeInsets.all(5),
                constraints: BoxConstraints(minHeight: 50),
                decoration: BoxDecoration(
                  color: Colors.purpleAccent,
                  borderRadius: BorderRadius.circular(5), //圆角
                  gradient: //渐变
                      LinearGradient(
                    colors: [Colors.red[50], Colors.yellow[100]],
                  ),
                ),
                child: Row(
                  children: [
                    IconButton(
                      icon: Icon(_isPlaying ? Icons.pause : Icons.play_arrow),
                      onPressed: () {
                        if (_isPlaying) {
                          _videoPlayerController.pause();
                        } else {
                          _videoPlayerController.play();
                        }
                      },
                    ),
                    Text(_playTime),
                    Expanded(
                      flex: 1,
                      child: Slider(
                        min: 0.0,
                        max: 100.0,
                        activeColor: Colors.white,
                        value: _slideValue,
                        onChanged: (value) {
                          setState(() {
                            this._slideValue = value;
                            int second = (value * _totalTimeSec) as int;
                            _videoPlayerController
                                .seekTo(Duration(seconds: second));
                          });
                        },
                      ),
                    ),
                    Text(_totalTime),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    _videoPlayerController.dispose();
    super.dispose();
  }
}
